package com.xz.courseschedule.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xz.courseschedule.common.dto.TimetableDto;
import com.xz.courseschedule.entity.Timetable;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 课程表接口
 */
@Mapper
public interface TimetableMapper extends BaseMapper<Timetable> {

    //删除某学期的全部信息
    @Delete("delete from timetable where semester_id = #{semesterId}")
    void deleteAll(@Param("semesterId") Long semesterId);

    //获取冲突列名列表
    List<String> getConflictingColumnNameList(Long semesterId,String columnName);

    //获取冲突时间的列表
    List<Long> getConflictingTimeslotIdList(Long semesterId, String columnName, String no);

    //获取配置列表
    List<TimetableDto> getConfilctList(Long id);

    //按班级查询课表信息
    @Select("SELECT timeslot, class_no, course_no, teacher_no, room_no FROM timetable WHERE class_no = #{classNo} GROUP BY timeslot, class_no, course_no, teacher_no, room_no")
    List<TimetableDto> getTimetableByClassNo(@Param("classNo") String classNo);

    // 获取各时间段教室使用情况
    @Select("SELECT " +
            "CASE " +
            "  WHEN timeslot IN (1,2) THEN '1-2节' " +
            "  WHEN timeslot IN (3,4) THEN '3-4节' " +
            "  WHEN timeslot IN (5,6) THEN '5-6节' " +
            "  WHEN timeslot IN (7,8) THEN '7-8节' " +
            "  WHEN timeslot IN (9,10,11) THEN '9-11节' " +
            "  ELSE '其他' " +
            "END as time_period, " +
            "COUNT(DISTINCT room_no) as room_count " +
            "FROM timetable " +
            "GROUP BY " +
            "CASE " +
            "  WHEN timeslot IN (1,2) THEN '1-2节' " +
            "  WHEN timeslot IN (3,4) THEN '3-4节' " +
            "  WHEN timeslot IN (5,6) THEN '5-6节' " +
            "  WHEN timeslot IN (7,8) THEN '7-8节' " +
            "  WHEN timeslot IN (9,10,11) THEN '9-11节' " +
            "  ELSE '其他' " +
            "END " +
            "ORDER BY MIN(timeslot)")
    List<Map<String, Object>> getRoomUsageByTimeSlot();
}
